<?php

define ( 'JOIN_METHOD', 0 );
define ( 'JOIN_TALBE1', 1 );
define ( 'JOIN_COLUMN1', 2 );

define ( 'JOIN_OPERATION', 3 );

define ( 'JOIN_TALBE2', 4 );
define ( 'JOIN_COLUMN2', 5 );

class descTable {
	public $Field;
	public $Type;
	public $Null;
	public $Key;
	public $Default;
	public $Extra;
	public $attribute;
	public $newField;
	public $newType;
	public $newTypeSize;
}

class TableObject {
	
	public $data;
	public $tableName;
	public $aliasName;
	public $columnSize;
}

include_once "configs/smartmain.php";
include_once "configs/Mysql.class.php";
include_once 'Util/MyStringUtil.php';



$tables = array ();
$totalTables = $_REQUEST ["totalTableSize"];

if (isset ( $_REQUEST ["middleWord"] )) {
	$middleWord = $_REQUEST ["middleWord"];
}
else {
	$middleWord = "";
}

$joinSetting = array ();
$tableNameAliasMap = array ();
$db = new DB ( );
for($i1 = 0, $i2 = 0; $i1 < $totalTables; $i1 ++) {
	$tn = "tables" . $i1;
	$an = "aliasName" . $i1;
	$joinMethod = 'join' . $i1 . '_method';
	$joinTable1 = 'join' . $i1 . '_table1';
	$joinTable2 = 'join' . $i1 . '_table2';
	$joinColumn1 = 'join' . $i1 . '_column1';
	$joinColumn2 = 'join' . $i1 . '_column2';
	$joinOperation = 'join' . $i1 . '_operation';
	//echo $tn."<br //>";
	

	if ($i1 > 0 && isset ( $_REQUEST [$joinTable1] ) && isset ( $_REQUEST [$joinTable2] ) && isset ( $_REQUEST [$joinMethod] ) && isset ( $_REQUEST [$joinOperation] ) && isset ( $_REQUEST [$joinColumn1] ) && isset ( $_REQUEST [$joinColumn2] )) {
		$joinSetting [] = array ($_REQUEST [$joinMethod], $_REQUEST [$joinTable1], $_REQUEST [$joinColumn1], $_REQUEST [$joinOperation], $_REQUEST [$joinTable2], $_REQUEST [$joinColumn2] );
	}
	else {
		$joinSetting [] = array ();
	}
	echo $i1." Table name:".$_REQUEST [$tn].",alias:".$_REQUEST [$an]."<br //>";
	if (isset ( $_REQUEST [$tn] ) && isset ( $_REQUEST [$an] )) {
		
		$temp = new TableObject ( );
		$temp->tableName = $_REQUEST [$tn];
		$temp->aliasName = $_REQUEST [$an];
		
		
		$temp->data = array ();
		$query = $db->query ( "desc " . $temp->tableName );
		
		//echo "start <p>";
		for($counter = 0;; $counter ++) {
			$row = $db->fetch_array ( $query );
			if (! $row) {
				break;
			}
			$one = new descTable ( );
			
			$one->Field = $row ["Field"];
			$one->Type = $row ["Type"];
			$one->Null = $row ["Null"];
			$one->Key = $row ["Key"];
			$one->Default = $row ["Default"];
			$one->Extra = $row ["Extra"];
			$cType = MyStringUtil::Tokenizer ( $one->Type, "()" );
			if (count ( $cType ) != 0) {
				$one->newType = $cType [0];
				if (isset ( $cType [1] )) {
					$one->newTypeSize = $cType [1];
				}
				else {
					$one->newTypeSize = 20;
				}
			}
			else {
				$one->newType = $one->Type;
				$one->newTypeSize = 24;
			}
			
			if ($temp->aliasName != "") {
				$one->attribute = $temp->aliasName . $middleWord . MyStringUtil::CoverColumnToAttribute ( $one->Field );
				$one->newField = $temp->aliasName . "." . $one->Field;
			}
			else {
				$one->attribute = MyStringUtil::CoverColumnToAttribute ( $one->Field );
				$one->newField = $one->Field;
			}
			$temp->data [$counter] = $one;
			
			//echo $counter."=".$temp->data[$counter]->Field."<br //>";
			$tableNameAliasMap [$_REQUEST [$tn]] = $_REQUEST [$an];
		
		}
		
		$temp->columnSize = count ( $temp->data );
		
		$tables [$i2] = $temp;
		$i2 ++;
	}
}
$db->close ();

//echo count($tableNames)."<br //>";
$fromStr = " FROM ";

for($i1 = 0; $i1 < count ( $tables ); $i1 ++) {
	if (count ( $joinSetting [$i1] ) == 6) {
		$fromStr .= ' ' . $joinSetting [$i1] [JOIN_METHOD] . ' ';
	}
	$fromStr .= '"._' . strtoupper ( $tables [$i1]->tableName ) . '_TABLE_NAME." ' . $tables [$i1]->aliasName;
	
	if (count ( $joinSetting [$i1] ) == 6) {
		$fromStr .= ' on ';
		$fromStr .= $tableNameAliasMap [$joinSetting [$i1] [JOIN_TALBE1]] . '.' . $joinSetting [$i1] [JOIN_COLUMN1];
		$fromStr .= ' ' . $joinSetting [$i1] [JOIN_OPERATION] . ' ';
		$fromStr .= $tableNameAliasMap [$joinSetting [$i1] [JOIN_TALBE2]] . '.' . $joinSetting [$i1] [JOIN_COLUMN2];
	}

}

$template->assign ( "tables", $tables );
$template->assign ( "tableSize", $totalTables );
$template->assign ( "fromStr", $fromStr );
$template->display ( 'ArrangeColumnAttributes.tpl.html' );

?>
